package com.doucat.support.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tonly.doucatlibrary.R;


/**
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * File: LoadingDialog.java
 * Author: win7
 * Version: V100R001C01
 * Create: 2019/6/12 16:02
 * Changes (from 2019/6/12)
 * -----------------------------------------------------------------
 * 2019/6/12 : Create LoadingDialog.java (win7);
 * -----------------------------------------------------------------
 *
 * ProgressDialog替代品，利用base的隐藏导航栏
 */
public class PbDialog extends BaseDialog {
    public static final int PERCENTAGE=0;
    public static final int SCALE=1;
    private TextView tvTitle;
    private TextView tvProgress;
    private ProgressBar pBar;
    private Button btnRight;
    private int progressFormat=SCALE;
    @Override
    public View onLoadLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_dialog_pb, null);
        tvTitle=view.findViewById(R.id.tvTitle);
        tvProgress=view.findViewById(R.id.tvProgress);
        btnRight=view.findViewById(R.id.btnRight);
        pBar =view.findViewById(R.id.pBar);

        tvTitle.setText(title);
        btnRight.setText(btnString);
        btnRight.setOnClickListener(onClickListener);

        return view;
    }

    public PbDialog setProgressFormat(int progressFormat) {
        this.progressFormat = progressFormat;
        return this;
    }

    private CharSequence title="";
    private CharSequence btnString ="";
    private View.OnClickListener onClickListener;

    public PbDialog setProgress(int max, int progress){
        if(pBar!=null){
            pBar.setMax(max);
            pBar.setProgress(progress);
        }
        if(tvProgress!=null){
            if(progressFormat==PERCENTAGE){
                int value=progress*100/max;
                tvProgress.setText(value+"%");
            }else if(progressFormat==SCALE){
                tvProgress.setText(progress+"/"+max);
            }
        }
        return this;
    }

    public PbDialog setButton(CharSequence text, final View.OnClickListener listener) {
        this.btnString =text;
        this.onClickListener =listener;
        return this;
    }

    public void showFinish() {
        btnRight.setText(getContext().getString(R.string.finish));
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public PbDialog setTitle(String string) {
        this.title=string;
        return this;
    }

    public PbDialog setPbIndeterminate(boolean b) {
        if(pBar!=null){
            pBar.setIndeterminate(b);
        }
        return this;
    }

}
